﻿using System; 
using System.Security.Principal;
using System.ServiceModel;
using DataAccessLayer;
using System.Collections;
using System.Collections.Generic;
using DTO;
using DTO.Reports;

namespace IndexMarkWcfService
{
    public class SecurityController
    {
        public static readonly SecurityController Instance = new SecurityController();

        public String GetUserName()
        {
            //try
            //{
                #if DEBUG                
                    return WindowsIdentity.GetCurrent().Name;
                #else
                    return ServiceSecurityContext.Current.WindowsIdentity.Name;
                #endif
            //}
            //catch (Exception e)
            //{
            //    return e.Message;
            //}
        }

        //private bool HasEditDate(int IDTerm)
        //{
        //    IDataProvider context = ProviderFactory.CreateProvider();
        //    return context.HasEditData(IDTerm);
        //}

        private IdentityReferenceCollection GetIdentityReferenceCollection()
        {
            #if DEBUG
                return WindowsIdentity.GetCurrent().Groups;
            #else
                return ServiceSecurityContext.Current.WindowsIdentity.Groups;
            #endif
        }

        public bool IsSupervisor()
        {
            #if DEBUG
                        return true;
            #endif
            
            IdentityReferenceCollection list = GetIdentityReferenceCollection();

            IDataProvider context = ProviderFactory.CreateProvider();
            String SID = context.GetSupervisorSID();

            foreach (IdentityReference item in list)
            {
                if (item.Value == SID)
                {
                    return true;
                }
            }
            return false;
        }

        public IEnumerable<String> GetRoleList()
        {
            IdentityReferenceCollection list = GetIdentityReferenceCollection();

            IList<String> ListSID = new List<string>();
            foreach (IdentityReference item in list)
            {
                ListSID.Add(item.Value);
            }

            IDataProvider context = ProviderFactory.CreateProvider();
            IList<string> Roles = context.GetRoles(ListSID) as IList<string>;
            if (Roles.Count == 0) Roles.Add("Нет ролей");

            return Roles;
        }

        public IEnumerable<String> GetSIDs()
        {
            IdentityReferenceCollection list = GetIdentityReferenceCollection();

            IList<String> ListSID = new List<string>();
            foreach (IdentityReference item in list)
            {
                ListSID.Add(item.Value);
            }

            return ListSID;
        }

        public IEnumerable<RespondentAnswers> GetVisibilityAnswers(List<RespondentAnswers> answers, int IDCharTerm)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            bool IsSupervisor = this.IsSupervisor();
            
            bool IsResponsible = context.IsResponsible(IDCharTerm, this.GetSIDs());
            
            bool IsVisibleForAll = context.IsVisibleForAll(IDCharTerm);

            foreach (RespondentAnswers RA in answers)
            {
                foreach (Answer A in RA.Answers)
                {
                    if (!IsSupervisor && !IsResponsible && !IsVisibleForAll)
                    {
                        A.Volume = null;
                    }
                }
            }
            return answers;
        }

        public IEnumerable<VolumePPS> GetVisibilityVolums(List<VolumePPS> volums, int IDCharGrad)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            List<VolumePPS> list = new List<VolumePPS>();
            bool IsSupervisor = this.IsSupervisor();
            //int IDChar = context.GetCharID(IDCharGrad);
            //int IDTerm = context.GetTermID(IDCharGrad);
            //bool IsResponsible = context.IsResponsible(IDChar, IDTerm, this.GetSIDs());
            //bool IsVisibleForAll = context.IsVisibleForAll(IDChar, IDTerm);

            bool IsResponsible = true;
            bool IsVisibleForAll = true;

            foreach (VolumePPS VPPS in volums)
            {
                if (!IsSupervisor && !IsResponsible && !IsVisibleForAll)
                {
                    VPPS.Name = null;
                    VPPS.Volume = null;
                }
                list.Add(VPPS);
            }
            return list;
        }

        public VolumsPPS GetVisibilityVolums(VolumsPPS volums)
        {
            IDataProvider context = ProviderFactory.CreateProvider();

            bool IsSupervisor = this.IsSupervisor();

            // int IDChar = context.GetCharID(volums.IDCharGrad);
            // int IDTerm = context.GetTermID(volums.IDCharGrad);
            //bool IsResponsible = context.IsResponsible(IDChar, IDTerm, this.GetSIDs());
            //bool IsVisibleForAll = context.IsVisibleForAll(IDCharTerm);

            bool IsResponsible = true;
            bool IsVisibleForAll = true;

            foreach (VolumePPS VPPS in volums.Volums)
            {
                if (!IsSupervisor && !IsResponsible && !IsVisibleForAll)
                {
                    VPPS.VolumeWithWeight = null;
                    VPPS.Volume = null;
                }
            }

            return volums;
        }

        public void AddInputLog()
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            context.AddInputLog(this.GetUserName());
        }


        public IEnumerable<Permission> GetPermissions(int IDTerm)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            return context.GetPermissions(GetSIDs(), IDTerm);
        }

        public bool HasSaveAnswers(int IDCharTerm, int IDResp)
        {
            IDataProvider context = ProviderFactory.CreateProvider();
            bool IsSupervisor = this.IsSupervisor();
            bool IsResponsible = context.IsResponsible(IDCharTerm, IDResp, this.GetSIDs());

            return IsSupervisor || IsResponsible;
        }

        public string UserSID
        {
            get
            {
                return WindowsIdentity.GetCurrent().User.Value;
            }
        } 
    }
}
